package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.R;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.link.view.adapter.PickAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

/**
 * 知识产权pop
 */
public class IPRPopWindow extends BasePopWindow {

    private MaxHeightRecyclerView recyclerView;
    private TextView okView;
    private TextView resetView;
    private LinearLayout bottomLayout;
    private PickAdapter pickAdapterCity;
    private List<PickSelectEntity> cityDatas;
    private String no = "";
    private String name = "";
    private int mPosition = -1;

    public IPRPopWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_area_ipr;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.rlv_list);
        bottomLayout = holder.getPop().findViewById(R.id.bottom_layout);

        pickAdapterCity = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        pickAdapterCity.setItemSelect(true);
        pickAdapterCity.setDefItem(0);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        recyclerView.setAdapter(pickAdapterCity);
        pickAdapterCity.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                no = pickSelectEntity.getNo();
                name = pickSelectEntity.getProjectscale();
                mPosition = position;
                if (onResultClick != null) {
                    onResultClick.result(no, name, position);
                    //Log.d("城市编号", no);
                }
                dissMiss();
            }
        });
        holder.getPop().findViewById(R.id.cover_layout).setOnClickListener(view -> dissMiss());
    }

    public void setData(List<PickSelectEntity> cityDatas) {
        this.cityDatas = cityDatas;
        pickAdapterCity.setDatas(cityDatas);

    }

    public void setData(List<PickSelectEntity> cityDatas, int position) {
        mPosition = position;
        this.cityDatas = cityDatas;
        pickAdapterCity.setDatas(cityDatas);
        pickAdapterCity.setDefItem(position);
    }

    public void setVisibleList(boolean visible) {
        recyclerView.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
    }

    public void setDefData(int position) {
        pickAdapterCity.setDefItem(position);
    }

    public int getPosition() {
        return mPosition;
    }

    public List<PickSelectEntity> getData() {
        return pickAdapterCity.getDatas();
    }

    public PickSelectEntity getResult() {
        if (mPosition == -1) {
            return null;
        }
        return pickAdapterCity.getDatas().get(mPosition);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (onDisMiss != null)
            onDisMiss.disMiss();
    }
}
